import time
from common.unit import AndroidUI
from case.lnitial.lnitial_page import LnitialTest
from appium.webdriver.common.touch_action import TouchAction

#系统设置里面的按钮(判断是否开启固定箱码，科室批量和原来的不一样)
class SystemObject:

    def __init__(self):
        self.driver = AndroidUI.get_webdriver()
        self.home = LnitialTest()
        # self.home.input_true_password()     #输入正确的密码，并且跳转到系统设置界面

    # 服务器选择
    def server_button(self):
        # element = self.driver.find_element_by_xpath(
        #     "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout"
        #     "/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout"
        #     "/android.widget.LinearLayout[6]/android.widget.Spinner[1]/android.widget.TextView")
        # element = self.driver.find_element_by_android_uiautomator('new UiSelector().text("服务器选择")')
        element = self.driver(text="服务器选择")
        # el=   self.driver.find_element_by_android_uiautomator('new UiSelector().text("请输入管理员密码")'):
        return element

    # 服务器选择浙江
    def choice_server(self, city):
        # element = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.ListView"
        #                                         "/android.widget.TextView[2]")
        # element = self.driver.find_element_by_android_uiautomator('new UiSelector().text("%s")' % city)
        element = self.driver(text=f"{city}")
        return element

    # 确定按钮
    def ok_button(self):
        element = self.driver(resourceId="com.bluephone:id/md_buttonDefaultPositive")
        return element

    # 机型选择
    def type_button(self):
        # element = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout"
        #                                             "/android.widget.LinearLayout/android.widget.FrameLayout/"
        #                                             "android.widget.LinearLayout/android.widget.FrameLayout/"
        #                                             "android.widget.ScrollView/android.widget.LinearLayout/"
        #                                             "android.widget.LinearLayout[6]/android.widget.Spinner[2]/"
        #                                             "android.widget.TextView")
        # element = self.driver.find_element_by_android_uiautomator('new UiSelector().text("机型选择")')
        element = self.driver(text="机型选择")
        return element

    # 选择机型，type为类型名字
    def choice_type(self, type):
        # element = self.driver.find_element_by_android_uiautomator('new UiSelector().text("%s")' % type)
        element = self.driver(text=f"{type}")
        return element

    # 出码分离
    def not_together(self):
        # element = self.driver.find_element_by_android_uiautomator('new UiSelector().text("出码分离")')
        element = self.driver(text="出码分离")
        return element

    # 出码一体
    def together(self):
        # element = self.driver.find_element_by_android_uiautomator('new UiSelector().text("出码一体")')
        element = self.driver(text="出码一体")
        # element = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout"
        #                                             "/android.widget.ListView/android.widget.TextView[2]")
        return element

    # 纯扫描版
    def scan_version(self):
        # element = self.driver.find_element_by_android_uiautomator('new UiSelector().text("纯扫描版")')
        element = self.driver(text="纯扫描版")
        return element

    # -------------------------信息采集模式-----------------------------

    # 科室装袋
    def bag(self):
        element = self.driver(resourceId="com.bluephone:id/collectmode1")
        return element

    # 科室装箱
    def box(self):
        element = self.driver(resourceId="com.bluephone:id/collectmode4")
        return element

    # 溯源校准仪
    def cart_checke(self):
        element = self.driver(resourceId="com.bluephone:id/collectmode2")
        return element

    # -----------------------基础设置----------------------------

    # 滑动从（x，y）到（x2，y2）
    def swipe(self, x, y, x2, y2):
        TouchAction(self.driver).press(x=x, y=y).move_to(x=x2, y=y2).release().perform()
        # TouchAction(self.driver).press(x=348, y=659).move_to(x=370, y=131).release().perform()
        print('下滑成功')

    # 固定箱码开那个按钮的text（这边有修改的，返回的是字符串）
    def box_status(self):
        # element = self.driver.find_element_by_android_uiautomator('new UiSelector().text("固定箱码(打开)")')
        # element = self.driver(text="固定箱码(打开)")
        element = self.box_id().get_text()
        return element


    # 固定箱码关
    # def close_box(self):
    #     # element = self.driver.find_element_by_android_uiautomator('new UiSelector().text("固定箱码(关闭)")')
    #     # element = self.driver(textContains="固定箱码(关闭)")
    #     element = self.box_id().get_text()
    #     return element


    # 固定箱码id
    def box_id(self):
        element = self.driver(resourceId='com.bluephone:id/fixBoxQRCode')
        return element

    # 科室批量开那个按钮的text
    def open_batch(self):
        # element = self.driver.find_element_by_android_uiautomator('new UiSelector().text("科室批量(打开)")')
        # element = self.driver(text="科室批量(打开)")
        element = self.batch_id().get_text()
        return element

    # 科室批量关
    def close_batch(self):
        # element = self.driver.find_element_by_android_uiautomator('new UiSelector().text("科室批量(关闭)")')
        # element = self.driver(text="科室批量(关闭)")
        element = self.batch_id().get_text()
        return element

    # 科室批量id
    def batch_id(self):
        element = self.driver(resourceId='com.bluephone:id/batchCollect')
        return element

    # 返回首页
    def back_button(self):
        # self.driver.pressKeyCode(AndroidKeyCode.BACK)
        # self.driver.press_keycode()
        # self.driver.keyevent(4)
        self.driver.keyevent('back')
        # self.driver.press_keycode(AndroidKeyCode.BACK)
        # self.driver.press_keycode('KEYCODE_BACK')


if __name__ == '__main__':
    s = SystemObject()
    # 选服务器
    # s.server_button().click()
    # s.zhejiang_server().click()
    # s.ok_button().click()
    # print('选择服务器浙江成功')
    #
    # #选类型
    # LnitialTest().input_true_password()
    # s.type_button().click()
    # s.not_together().click()
    # s.ok_button().click()
    # print('机型选择成功')
    #
    # #信息采集模式修改
    # LnitialTest().input_true_password()
    # s.bag().click()
    # print('信息采集模式选择袋模式')
    # s.box().click()
    # print('信息采集模式选择箱模式')

    # 基础设置
    LnitialTest().input_true_password()
    time.sleep(1)
    s.driver.swipe_ext("up")

    # s.swipe(350, 652, 350, 100)
    # 打开固定箱码
    # try:
    #
    #     time.sleep(0.2)
    #     s.open_box()
    #     print('已经开启了固定箱码')
    # except:
    #     s.open_box().click()
    #     print('打开固定箱码成功')

    # 关闭固定箱码

    print(s.close_box())
    if s.close_box()=="固定箱码(关闭)":
        print('已经关闭')
    # try:
    #     # s.box_id().get_text()=="固定箱码(关闭)"
    #     # time.sleep(1)
    #     s.close_box()=="固定箱码(关闭)"
    #     print('已经关闭了固定箱码')
    else:
        s.box_id().click()
        print('关闭固定箱码成功')
    #
    # s.back_button()
    # print("返回首页成功")

    time.sleep(3)
    # s.driver.quit()
